package caseManagerBusiness;

import com.alibaba.fastjson.JSONArray;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/7/11.
 * 返回报告的时间集合
 * 暂时无法实现
 * select DISTINCT date_format(createtime,'%Y-%m-%d') as date from testreport ORDER BY createtime DESC
 */
public class LoadReportsData implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        //读取请求参数
        String action = request.getParameter("action");

        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        SqlReturnModel sqlReturnModel = new SqlReturnModel();
        //判断参数是否为空
        if (Objects.equals("getReportData", action)){
            String sqlWord = "select DISTINCT date_format(createtime,'%Y-%m-%d') as date from testreport ORDER BY createtime DESC";
            sqlReturnModel = sqlDispatch.selectWord(sqlWord);
        }


        if (sqlReturnModel.getIsSuccess()){
            returnJson.setApiStatus(1);
            JSONArray resultArr = new JSONArray();
            for (ArrayList<String> arrayLists : sqlReturnModel.getSelectReturnData()){
                resultArr.add(arrayLists.get(0));
            }
            returnJson.setResult(resultArr);
        }else {
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        return returnJson.toString();
    }
}
